<?php

declare(strict_types=1);

namespace Modules\User\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Auth;

/**
 * 管理员行为事件
 */
class AdminBehaviorEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * 操作行为
     */
    public string $action;

    /**
     * 操作内容
     */
    public array $behavior;

    /**
     * IP地址
     */
    public string $operateIp;

    /**
     * 操作者ID
     */
    public int $creatorId;

    /**
     * 创建新的事件实例
     *
     * @param string $action 操作行为
     * @param array $content 操作内容
     * @param string $ip IP地址
     * @param int $creatorId 操作者ID
     */
    public function __construct(string $action, array $behavior, string $ip, int $creatorId = 0) {
        $this->action = $action;
        $this->behavior = $behavior;
        $this->operateIp = $ip;
        $this->creatorId = $creatorId ?: (Auth::check() ? Auth::id() : 0);
    }

    /**
     * 获取广播频道
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('admin-behavior');
    }
}
